<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('用户列表')"/>
    <th:block th:include="include :: layout-latest-css"/>
    <th:block th:include="include :: ztree-css"/>
</head>
<body class="gray-bg">
<div id="west" class="ui-layout-west">

    <!--登录信息-->
    <input name="jigoujibie" th:value="${session.jigoujibie}" type="hidden" id="jigoujibie">
    <div class="box box-main">
        <div class="box-header">
            <div class="box-title">
                <i class="fa icon-grid"></i> 单位
            </div>
            <div class="box-tools pull-right">
                <button type="button" class="btn btn-box-tool" id="btnExpand" title="展开" style="display:none;"><i
                        class="fa fa-chevron-up"></i></button>
                <button type="button" class="btn btn-box-tool" id="btnCollapse" title="折叠"><i
                        class="fa fa-chevron-down"></i></button>
                <button type="button" class="btn btn-box-tool" id="btnRefresh" title="刷新单位"><i
                        class="fa fa-refresh"></i></button>
            </div>
        </div>
        <div class="ui-layout-content" style="margin-top: 15px;">
            <div id="tree" class="ztree"></div>
        </div>
    </div>
</div>

<div id="center" class="ui-layout-center">
    <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="user-form">
                    <input type="hidden" id="nodedeptId" name="olddeptid">
                    <input type="hidden" id="parentId" name="parentId">
                    <input name="deptid" th:value="${session.deptid}" type="hidden" id="deptid">
                    <div class="select-list">
                        <ul>
                            <li>
                                专家&nbsp;<input type="text" name="name"/>
                            </li>
                            <li class="sousuo">
                                <a class="btn btn-primary btn-sm" onclick="$.table.search()"><i
                                        class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-sm" onclick="chongzhi()"><i
                                        class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="col-sm-6 select-table-overwrite" style="overflow: auto;">
                <div style="text-align: center"><span style="font-size: large;color: black;">选择退款</span></div>
                <table id="bootstrap-table"></table>
            </div>
            <div class="col-sm-6 select-table-overwrite" style="overflow: auto;">
                <div style="text-align: center"><span style="font-size: large;color: black;">已选退款</span></div>
                <table id="bootstrap-table2"></table>
            </div>
            <div  class="col-sm-12">
                <div >
                    <h3 style="margin-top: 10px">退款原因</h3>
                    <textarea id="yuanyin" style="width: 100%;height: 100px"></textarea>
                </div>
            </div>
        </div>
    </div>
</div>

<th:block th:include="include :: footer"/>
<th:block th:include="include :: layout-latest-js"/>
<th:block th:include="include :: ztree-js"/>
<script th:inline="javascript">
    var prefix = ctx + "system/user";
    var ZJZXID = "000"
    var jieshourenlist = []

    var Treeurl = ctx + "system/user/TuikuandeptTree?deptid=" + $("#deptid").val();
    $(function () {
        var panehHidden = false;
        if ($(this).width() < 769) {
            panehHidden = true;
        }
        $('body').layout({initClosed: panehHidden, west__size: 0});
        // 回到顶部绑定
        if ($.fn.toTop !== undefined) {
            var opt = {
                win: $('.ui-layout-center'),
                doc: $('.ui-layout-center')
            };
            $('#scroll-up').toTop(opt);
        }
        queryUserList();
        queryDeptTree();
        $("#bootstrap-table2").bootstrapTable({
            pagination: false,
            height: 400,
            columns: [{
                field: 'id',
                title: 'ID',
                visible: false
            },
                {
                    field: 'deptid',
                    title: 'deptid',
                    visible: false
                },
                {
                    field: 'zhuanjiaid',
                    title: '专家ID',
                    visible: false
                },
                {
                    field: 'xingming',
                    title: '专家名称',
                    halign: "center",
                    align: "center"
                },
                {
                    field: 'nianyue',
                    title: '计发年月',
                    halign: "center",
                    align: "center"
                },
                {
                    field: 'jine',
                    title: '退款金额',
                    halign: "center",
                    align: "center"
                },
                {
                    field: 'remove',
                    title: '操作',
                    halign: "center",
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="removeSelect(' + row.id + ')"<i class="fa fa-remove">删除</i></a> ');
                        return actions.join('');
                    }
                }
            ],

        });
    });

    function queryUserList() {
        var options = {
            url: "/system/zhiliu/JingfeiZhiliuList",
            sortName: "deptid",
            sortOrder: "desc",
            modalName: "zhuanjia",
            showRefresh: false,
            showSearch: false,
            showToggle: false,
            showColumns: false,
            pagination: false,
            firstLoad: false,
            height: 420,
            columns: [{
                checkbox: true,
            },
                {
                    field: 'id',
                    title: 'ID',
                    visible: false
                },
                {
                    field: 'deptid',
                    title: 'deptid',
                    visible: false
                },
                {
                    field: 'tuikuanid',
                    title: 'tuikuanid',
                    visible: false
                },
                {
                    field: 'zhuanjiaid',
                    title: '专家ID',
                    visible: false
                },
                {
                    field: 'xingming',
                    title: '专家名称',
                    halign: "center",
                    align: "center"
                },
                {
                    field: 'nianyue',
                    title: '计发年月',
                    halign: "center",
                    align: "center"
                },
                {
                    field: 'jine',
                    title: '退款金额',
                    halign: "center",
                    align: "center"
                }
            ],
            onCheck: function (row, $element) {
                if (jieshourenlist.every(item => item.id != row.id)) {
                    jieshourenlist.push(row)
                }
                $("#bootstrap-table2").bootstrapTable('load', jieshourenlist);
            },
            onUncheck: function (row, $element) {
                jieshourenlist = jieshourenlist.filter(item => item.id != row.id)
                $("#bootstrap-table2").bootstrapTable('load', jieshourenlist);
            },
            onCheckAll: function (rows) {
                for (var row of rows) {
                    if (jieshourenlist.every(item => item.id != row.id)) {
                        jieshourenlist.push(row)
                    }
                }
                $("#bootstrap-table2").bootstrapTable('load', jieshourenlist);
            },
            onUncheckAll: function (rowsAfter, rowsBefore) {
                for (var row of rowsBefore) {
                    jieshourenlist = jieshourenlist.filter(item => item.id != row.id)
                }
                $("#bootstrap-table2").bootstrapTable('load', jieshourenlist);
            }
        };
        $.table.init(options);
    }

    function queryDeptTree() {
        var options = {
            url: Treeurl,
            expandLevel: 2,
            onClick: zOnClick,
            onCheck: zonCheck,
            check: {
                enable: true,             // 置 zTree 的节点上是否显示 checkbox / radio
                chkboxType: { "Y": "s", "N": "s" } // 父子节点的关联关系
            },
        };
        $.tree.init(options);

        function zOnClick(event, treeId, treeNode) {
            $("#nodedeptId").val(treeNode.id);
            $("#parentId").val(treeNode.mmpid);
            console.log(treeNode)
            $.table.search();
        }

        function zonCheck(event, treeId, treeNode) {
            var list = new Array(0);
            var parentidlist = new Array(0);
            if (treeNode.isParent == true) {
                getAllchild(treeNode, list)
                getAllchild(treeNode, parentidlist)
            }
            list.push(treeNode.id)
            parentidlist.push(treeNode.mmpid)
            $.ajax({
                type: "post",
                url: "/system/tuikuan/deptTreeZhuanjia",
                data: {
                    deptids: list.join(","),
                    parentids: parentidlist.join(","),
                    deptid:$("#deptid").val()
                },
                success: function (result) {
                    if (result.code == 0) {
                        for (var row of result.data) {
                            if (treeNode.checked == true) {
                                if (jieshourenlist.every(item => item.id != row.id)) {
                                    jieshourenlist.push(row)
                                }
                            } else {
                                jieshourenlist = jieshourenlist.filter(item => item.id != row.id)
                            }
                        }
                        $("#bootstrap-table2").bootstrapTable('load', jieshourenlist);
                    } else {
                        $.modal.alertError(result.msg);
                    }
                },
                error: function (error) {
                    $.modal.alertWarning("勾选失败");
                }
            });
        }
    }

    $('#btnExpand').click(function () {
        $._tree.expandAll(true);
        $(this).hide();
        $('#btnCollapse').show();
    });

    $('#btnCollapse').click(function () {
        $._tree.expandAll(false);
        $(this).hide();
        $('#btnExpand').show();
    });

    $('#btnRefresh').click(function () {
        queryDeptTree();
    });


    function getAllchild(treeNode,list){
        if(treeNode.isParent==true) {
            treeNode.children.forEach((item) => {
                list.push(item.id)
                getAllchild(item,list)
            })
        }else{
            list.push(treeNode.id)
        }
    }
    function removeSelect(id) {
        jieshourenlist = jieshourenlist.filter(item => item.id != id)
        $("#bootstrap-table2").bootstrapTable('load', jieshourenlist);
    }

    function chongzhi() {
        $("#nodedeptId").val('');
        $("#parentId").val('');
        $.form.reset()
    }

    function submit() {
        if(jieshourenlist.length==0){
            layer.alert("请选择退款的专家月份")
            return
        }
        var ids = ""
        var tuikuanids = ""
        jieshourenlist.every(item =>  (ids=ids+item.id+",",tuikuanids=tuikuanids+item.beizhu10+","))
        ids = ids.substring(0, ids.length - 1);
        tuikuanids = tuikuanids.substring(0, tuikuanids.length - 1);
        $.post('/system/jisuan/tuikuan', {
            ids : ids,
            yuanyin : $("#yuanyin").val(),
        },function(ppData) {
            if (ppData != null) {
                if (ppData.result == "1"){
                    layer.open({
                        time: 1000,
                        btn: [],
                        content: "退款成功!",
                        end: function (layero, index) {
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);//关闭当前页
                            vm_jieyujingfeituihui.refresh();
                        }
                    });

                }else{
                    layer.alert(ppData.message);
                }
            }
        },"json");
    }
</script>
</body>

</html>
<style>
    .select-table-overwrite {
        background: #fff;
        border-radius: 6px;
        margin-top: 10px;
        padding-top: 5px;
        padding-bottom: 13px;
        box-shadow: 1px 1px 3px rgba(0, 0, 0, .2);
    }

    #west {
        width: 350px !important;
    }

    #center {
        width: 1050px !important;
        position: relative !important;
    }

    .disabled {
        background-color: lightgrey !important;
    }

    .fixed-table-toolbar {
        display: none;
    }
</style>